package com.app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.adapter.ClassmateListAdapter;
import com.app.bean.ClassmateListBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ClassmateListActivity extends AppCompatActivity {

    private ListView lvClassmate;
    private ImageView ivBack;
    private String className = "18移动1班";
    private TextView tvLayoutTitle;
    private List<ClassmateListBean.ListBean> listBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classmate_list);
        initView();
        initData();
        initEvent();

    }


    private void initData() {
        String url = "http://www.xhjjyst.com/freshmenapp/classmate/listbyclassname";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("className",className);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(ClassmateListActivity.this);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject,new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                ClassmateListBean classmateListBean = gson.fromJson(jsonObject.toString(),ClassmateListBean.class);
                listBean = classmateListBean.getList();
                ClassmateListAdapter classmateListAdapter = new ClassmateListAdapter(ClassmateListActivity.this,(ArrayList<ClassmateListBean.ListBean>) listBean);
                lvClassmate.setAdapter(classmateListAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ClassmateListActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);
    }

    private void initView() {
        lvClassmate = findViewById(R.id.lv_classmate);
        ivBack = findViewById(R.id.iv_back);
        tvLayoutTitle = findViewById(R.id.tv_layout_title);
        tvLayoutTitle.setText(className);
    }

    private void initEvent() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ClassmateListActivity.this,MainActivity.class));
            }
        });
        lvClassmate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ClassmateListActivity.this,ClassmateDetailActivity.class);
                intent.putExtra("id", listBean.get(position).getId());
                startActivity(intent);
            }
        });
    }


}